package com.kaizzi.data.jdl.helper;

import com.kaizzi.data.common.been.TempBeen;
import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.common.utils.LogicUtil;
import com.kaizzi.data.jdl.model.ExcelModel;
import com.kaizzi.data.jdl.service.TaskLogsService;
import lombok.Data;
import org.springframework.stereotype.Component;
import org.springframework.util.unit.DataUnit;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/16
 */
@Component
public class PageData34 {

    @Resource
    private TaskLogsService taskLogsService;

    public String getData(ExcelModel model){

        Page34 page34 = new Page34();

        List<String> dateList = DateUtil.getDateList(1);
        List<String> transTime = DateUtil.getTransTime(dateList.get(0));
        List<TempBeen> gClubPoint = taskLogsService.getGClubPoint(transTime.get(0), transTime.get(1));
//        System.out.println(JsonUtil.serialize(gClubPoint));
        //名称转换
        gClubPoint = setRealName(gClubPoint);

        //日期集合
        Map<String, List<TempBeen>> collect = gClubPoint.stream().collect(Collectors.groupingBy(TempBeen::getTempData));

        for (String date : getDateList()) {
            if (collect.containsKey(date)){
                continue;
            }
            for (String wesShop : getWays()) {
                TempBeen tempBeen = new TempBeen();
                tempBeen.setTempCount(0);
                tempBeen.setTempName(wesShop);
                tempBeen.setTempData(date);
                gClubPoint.add(tempBeen);
            }
        }
        //名称集合
        Map<String,List<TempBeen>> result = gClubPoint.stream().collect(Collectors.groupingBy(TempBeen::getTempName));

        //先补全月份，再补全店铺
        for (String wesShop : getWays()) {
            List<TempBeen> beens = result.get(wesShop);
            if (beens != null && getDateList().size() == beens.size()){
                continue;
            }
            List<String> dateList1 = getDateList();
            List<String> haveList = new ArrayList<>();
            //需要进行非空验证
            if (beens != null){
                for (TempBeen been : beens) {
                    haveList.add(been.getTempData());
                }
            }
            List<String> collect1 = dateList1.stream().filter(item -> !haveList.contains(item)).collect(toList());
            for (String s : collect1) {
                TempBeen tempBeen = new TempBeen();
                tempBeen.setTempName(wesShop);
                tempBeen.setTempCount(0);
                tempBeen.setTempData(s);
                gClubPoint.add(tempBeen);
            }
        }

        //zengjia total
        Map<String, List<TempBeen>> nameMap = gClubPoint.stream().collect(Collectors.groupingBy(TempBeen::getTempName));
        for (String s : nameMap.keySet()) {
            List<TempBeen> beens = nameMap.get(s);
            TempBeen tempBeen = new TempBeen();
            tempBeen.setTempName(s);
            tempBeen.setTempData("人次总计");
            tempBeen.setTempCount(beens.stream().mapToLong(TempBeen::getTempCount).sum());
            gClubPoint.add(tempBeen);
        }

        Map<String, List<TempBeen>> dataMap = gClubPoint.stream().collect(Collectors.groupingBy(TempBeen::getTempData));
        for (String s : dataMap.keySet()) {
            List<TempBeen> beens = dataMap.get(s);
            TempBeen tempBeen = new TempBeen();
            tempBeen.setTempName("人数总计");
            tempBeen.setTempData(s);
            tempBeen.setTempCount(beens.stream().mapToLong(TempBeen::getTempCount).sum());
            gClubPoint.add(tempBeen);
        }

        page34.setTitle(LogicUtil.getTitle(model,34));
        page34.setBeens(gClubPoint);
        page34.setDataPeriod(DateUtil.getLastWeekDate());

        return JsonUtil.serialize(page34);
    }

    private List<String> getWays(){
        return Arrays.asList("每日签到","运动打卡","运动步数换积分","阅读文章并通知好友","新用户注册",
                "连续7天签到","GET你的战力简报","完善个人信息","购买商品");
    }

    private List<TempBeen> setRealName(List<TempBeen> list){
        List<TempBeen> tempBeens = new ArrayList<>();
        for (TempBeen tempBeen : list) {
            if (tempBeen.getTempName().equals("完成打卡并保存海报")){
                tempBeen.setTempName("运动打卡");
            }
            if (!getWays().contains(tempBeen.getTempName())){
                continue;
            }
            tempBeens.add(tempBeen);
        }
        return tempBeens;
    }

    //riqi
    private List<String> getDateList(){
        return DateUtil.getDateValue();
    }


    @Data
    private static class Page34{
        private String title;
        private List<TempBeen> beens;
        private String dataPeriod;
    }
}
